import config from '@/utils/config/index.js'
import request from '@/utils/request.js'

/**
 * 获取缺陷列表
 */
export function getDefectList(params, pager) {
	return request.get({
		url: '/workorder/v3/api/defectInfo/list',
		pager,
		params
	});
}

/**
 * 获取缺陷详情
 */
export function getDefectInfo(dfId) {
	return request.get({
		url: '/workorder/v3/api/defectInfo/init',
		params: { dfId }
	});
}

/**
 * 获取缺陷详情-根据缺陷编号
 */
export function getDefectInfoByNo(dfNo) {
	return request.get({
		url: '/workorder/v3/api/defectInfo/init',
		params: { dfNo }
	});
}

/**
 * 新增缺陷
 */
export function addDefect(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/add',
		params,
		paramType: 'post'
	});
}

/**
 * 删除缺陷
 */
export function deleteDefect(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/deleteInfo',
		params,
		paramType: 'post'
	});
}

/**
 * 获取缺陷等级
 */
export function getDefectLevel() {
	return request.get({
		url: '/workorder/v3/api/defectLevel/getLevelData',
		loading: false
	});
}

/**
 * 缺陷等级列表查询
 */
export function getDefectLevelList(params) {
	return request.get({
		url: '/workorder/v3/api/defectLevel/list',
		params
	});
}

/**
 * 根据缺陷等级获取通知人和消缺时间
 */
export function getNotifierByLevel(dfLevelStr) {
	return request.get({
		url: '/workorder/v3/api/defectLevel/getNotifierByLevel',
		params: { dfLevelStr }
	});
}

/**
 * 更新缺陷状态
 */
export function updateDefectStatus(dfId, state) {
	const params = {
		dfId: dfId,
		state: state
	}
	return request.get({
		url: '/workorder/v3/api/defectInfo/updateDefectStatus',
		params
	});
}

/**
 * 缺陷登记(包含工单处理)
 */
export function addAndOrder(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/addAndOrder',
		params,
		paramType: 'post'
	});
}

/**
 * 新增缺陷变更记录
 */
export function addChangeRecord(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/addChangeRecord',
		params,
		paramType: "post"
	});
}

/**
 * 获取审批操作信息
 */
export function getApproveOperateInfo(dfId, changeType) {
	const params = {
		dfId,
		changeType
	}
	return request.get({
		url: '/workorder/v3/api/defectInfo/getApproveOperateInfo',
		params
	});
}

/**
 * 新增缺陷审批记录
 */
export function addChangeApproveRecord(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/addChangeApproveRecord',
		params,
		paramType: "post"
	});
}

/**
 * 更新缺陷信息
 */
export function updateDefectInfo(params) {
	return request.post({
		url: '/workorder/v3/api/defectInfo/update',
		params,
		paramType: 'post'
	});
}

/**
 * 创建工作票
 */
export function addWorkTicket(params){
	return request.post({
		url: '/workorder/v3/api/dfWorkpicket/add',
		params,
		paramType: 'post'
	})
}

/**
 * 工作票终结,缺陷自动终结
 */
export function workTicketHasFinished(wtId){
	const params = {
		wtId
	}
	return request.post({
		url: '/workorder/v3/api/defectInfo/end',
		params,
	})
}

/**
 * 转专业通过更新缺陷信息
 */
export function changeMajor(dfId, type){
	return request.post({
		url: `/workorder/v3/api/defectInfo/changeMajor/${dfId}/${type}`
	})
}

// 离线缺陷上传
export async function uploadDefect(params) {
	let result = await uni.request({
		url: config.baseUrl + '/workorder/v3/api/defectInfo/add',
		data: params,
		method: 'POST',
		header: {
			'X-Authorization-access_token': uni.getStorageSync("_app_token")
		}
	})
	if(result[1]) {
		return result[1].data
	}
}

/**
 * 缺陷流程配置-详细信息
 */
export function getFlowConfig(dfType) {
	return request.get({
		url: '/workorder/v3/api/dfFlowConfig/detail',
		params: { dfType },
		loading: false
	});
}